<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="序列号">
              <a-input placeholder="请输入序列号" v-model="queryParam.number"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单号">
              <a-input placeholder="请输入订单号" v-model="queryParam.orderNum"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">

      <a-button type="primary" icon="download" @click="handleExportXls('序列号')">导出</a-button>
      <a-dropdown>
        <a-menu slot="overlay">
          <a-menu-item key="0" @click="handleShelvesBatch(0)">禁止</a-menu-item>
          <a-menu-item key="1" @click="handleShelvesBatch(1)">恢复</a-menu-item>
        </a-menu>
        <a-button type="primary" style="margin-left: 8px"> 禁止恢复 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

       <!--    <a-button type="primary"  @click="handleCeshi">测试</a-button>-->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">绑定的设备</a>
           <a-divider type="vertical" v-if='record.status==0||record.status==1'/>
           <a @click="handleShelves(record,1)" v-if='record.status==0'>恢复</a>
           <a @click="handleShelves(record,0)" v-if='record.status==1' style='color: red'>禁止</a>
        </span>

        <span slot="params" slot-scope="text, record">
            <a @click="gotoPage(record)">平台参数配置</a>
        </span>

        <span slot="termId" slot-scope="text, record, index">
            <a-tag color="pink" v-if="text==31">1个月</a-tag>
            <a-tag color="red" v-if="text==93" >3个月</a-tag>
           <a-tag color="orange" v-if="text==186" >6个月</a-tag>
           <a-tag color="green" v-if="text==366" >1年</a-tag>
           <a-tag color="cyan" v-if="text==732" >2年</a-tag>
           <a-tag color="blue" v-if="text==1098" >3年</a-tag>
        </span>

        <span slot="status" slot-scope="text, record, index">
            <a-tag color="red" v-if="text==0">禁止</a-tag>
            <a-tag color="#87d068" v-if="text==1" >正常</a-tag>
            <a-tag color="pink" v-if="text==2" >已失效</a-tag>
        </span>



      </a-table>
    </div>
    <order-platform-modal  ref="modalForm2" @ok="modalFormOk"></order-platform-modal>
    <order-arm-modal ref="modalForm3" @ok="modalFormOk"></order-arm-modal>
    <serial-number-modal ref="modalForm" @ok="modalFormOk"></serial-number-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import SerialNumberModal from './modules/SerialNumberModal'
  import OrderPlatformModal from './modules/OrderPlatformModal'
  import OrderArmModal from './modules/OrderArmModal'
  import { putAction, postAction, getAction } from '@api/manage'

  export default {
    name: 'SerialNumberList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      OrderPlatformModal,
      SerialNumberModal,
      OrderArmModal
    },
    data () {
      return {
        description: '序列号管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'序列号',
            align:"center",
            dataIndex: 'number'
          },
          {
            title:'有效期',
            align:"center",
            dataIndex: 'termId',
            scopedSlots: { customRender: 'termId' }
          },
          {
            title:'开始时间',
            align:"center",
            dataIndex: 'beginTime'
          },
          {
            title:'结束时间',
            align:"center",
            dataIndex: 'endTime'
          },
        {
          title:'订单号',
          align:"center",
          dataIndex: 'orderNum'
        },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title:'已绑定机器数量',
            align:"center",
            dataIndex: 'machineNum'
          },
          {
            title: '参数配置',
            dataIndex: 'params',
            align:"center",
            scopedSlots: { customRender: 'params' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/gongqiang/serialNumber/list",
          delete: "/gongqiang/serialNumber/delete",
          deleteBatch: "/gongqiang/serialNumber/deleteBatch",
          exportXlsUrl: "/gongqiang/serialNumber/exportXls",
          importExcelUrl: "gongqiang/serialNumber/importExcel",
          shelves:"/gongqiang/serialNumber/shelves",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        if(this.$route.query.orderNum!=null){
          this.queryParam.orderNum = this.$route.query.orderNum
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      gotoPage(record)
      {
        this.$router.push({
          path: '/igongqiang/numberPlatformList',
          query: { number: record.number, numberId: record.id,
            goodId:record.goodId,orderNum:record.orderNum
          }
        });
      },
      handleShelves(record,status)
      {
        if(!this.url.shelves){
          this.$message.error("请设置url.shelves!")
          return
        }
        var ids = record.id;
        var that = this;
        this.$confirm({
          title: "确认操作",
          content: "是否操作选中数据?",
          onOk: function () {
            that.loading = true;
            putAction(that.url.shelves, {ids: ids,status:status}).then((res) => {
              if (res.success) {
                //重新计算分页问题
                that.reCalculatePage(that.selectedRowKeys.length)
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      },
      handleShelvesBatch: function (key) {
        if(!this.url.shelves){
          this.$message.error("请设置url.shelves!")
          return
        }
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            ids += this.selectedRowKeys[a] + ",";
          }
          var that = this;
          this.$confirm({
            title: "确认操作",
            content: "是否操作选中数据?",
            onOk: function () {
              that.loading = true;
              putAction(that.url.shelves, {ids: ids,status:key}).then((res) => {
                if (res.success) {
                  //重新计算分页问题
                  that.reCalculatePage(that.selectedRowKeys.length)
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
      orderPlatform(record)
      {
        this.$refs.modalForm2.edit(record)
      },
      orderArm(record)
      {
        this.$refs.modalForm3.edit(record)
      },
 /*     handleCeshi(){
        postAction("/gongqiang/machineSerialNumber/checkSerialNumber", {serialNumber: "DSCSDDG5JEJJVA3123CF",machineNum:"5CD2_E413_7180_5917.",goodCode:"A01"}).then((res) => {
          if (res.success) {
            //重新计算分页问题

          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.loading = false;
        });
      },*/
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'number',text:'序列号',dictCode:''})
        fieldList.push({type:'string',value:'termId',text:'有效期',dictCode:''})
        fieldList.push({type:'datetime',value:'beginTime',text:'开始时间'})
        fieldList.push({type:'datetime',value:'endTime',text:'结束时间'})
        fieldList.push({type:'string',value:'status',text:'状态（1有效 2过期）',dictCode:''})
        fieldList.push({type:'string',value:'machineNum',text:'已绑定机器数量',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>